package com.oneday.ashop.webgate.user;

import com.oneday.ashop.core.base.mongo.BaseMongoDaoSupport;
import com.oneday.ashop.core.base.util.StringUtil;
import com.oneday.ashop.core.entity.Suggestion;
import com.oneday.ashop.core.entity.User;
import com.oneday.ashop.core.entity.book.Blog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor;

import java.util.List;

/**
 * User:bob.yang
 * Date 2015/7/15 0015
 */
@Repository
public class UserDao extends BaseMongoDaoSupport{

    /**
     * 查找用户
     * @return
     */
    public User findUser(Integer userId, String...fields) {
        Query query = Query.query(Criteria.where("_id").is(userId));
        includes(query, fields);
        return findOne(query, User.class);
    }

    /**
     * 查找用户
     * @param account
     * @param fields
     * @return
     */
    public User findUser(String account, String...fields) {
        Query query = Query.query(new Criteria().orOperator(Criteria.where("email").is(account), Criteria.where("telephone").is(account)));
        includes(query, fields);
        return findOne(query, User.class);
    }

    /**
     * 添加用户
     * @param user
     */
    public void addUser(User user) {
        insert(user);
    }

    /**
     * 添加网站评价
     * @param suggestion
     */
    public void addSuggestion(Suggestion suggestion) {
        insert(suggestion);
    }


    /**
     * 添加用户
     * @param name
     * @param password
     */
    public void addUser(String name, String password, String address) {
        User user = new User();
        user.setName(name);
        user.setPassword(password);
        if (!StringUtil.isEmpty(address)) {
            user.setAddress(address);
        }

        insert(user);
//        Query query = new Query(Criteria.where("_id").is(1));
//        List<User> list = mongoTemplate.find(query, User.class);
    }


    /**
     * 判断该账号是否存在
     * @param name
     * @return
     */
    public boolean queryUserByName(String name) {
        Query query = new Query(Criteria.where("name").is(name));
        includes(query,"_id");
        List<User> list = find(query, User.class);

        if (!list.isEmpty()) {
            return true;
        }
        return false;
    }

    /**
     * 根据账号删除用户
     * @param name
     */
    public void deleteUser(String name) {
        Query query = new Query(Criteria.where("name").is(name));
        delete(query, User.class);
    }

    public void updateUser(String name, String password, String address) {
        Query query = new Query(Criteria.where("name").is(name));
        Update update = new Update();

        if (!StringUtils.isEmpty(name)) {
            update.set("name", name);
        }
        if (!StringUtils.isEmpty(password)) {
            update.set("password", password);
        }
        if (!StringUtils.isEmpty(address)) {
            update.set("address", address);
        }

        updateAll(query, update, User.class);
    }

    public List<User> queryUser(String password) {
        Query query = new Query();
        if (!StringUtils.isEmpty(password)) {
            Criteria criteria = Criteria.where("password").is(password);
            query.addCriteria(criteria);
        }
        includes(query, "name", "password", "address");
        List<User> userList = find(query, User.class);
        return userList;
    }

    /**
     * 根据用户号更新用户信息
     * @param id
     * @param name
     */
    public void update(Integer id, String name) {
        Query query = new Query(Criteria.where("_id").is(id));
        //TODO 大刀 用法比较灵活
        Update update = Update.update("name", name);
        updateFirst(query, update, User.class);
    }

    /**
     * 更新头像
     * @param uid
     * @param imgUrl
     */
    public void updateImgUrl(Integer uid, String imgUrl) {
        Query query = Query.query(Criteria.where("_id").is(uid));
        Update update = Update.update("imgUrl", imgUrl);
        updateFirst(query, update, User.class);
    }

}
